package com.bootdo.app.common.utils;
//
//import java.util.Iterator;
//import java.util.Map;
//import java.util.Random;
//import java.util.Set;
//import java.util.SortedMap;
//import java.util.TreeMap;
//
//import com.alibaba.fastjson.JSON;
//
//
//
//
///**
// * 微信调起支付类
// *
// * @author Sunlight
// *
// */
//public class WXPay {
//	public static String createPackageValue(String appid, String appKey, String prepay_id)  {
//		SortedMap<String, String> nativeObj = new TreeMap<String, String>();
//		nativeObj.put("appId", appid);
//		nativeObj.put("timeStamp", OrderUtil.GetTimestamp());
//		Random random = new Random();
//		String randomStr = MD5.GetMD5String(String.valueOf(random.nextInt(10000)));
//		nativeObj.put("nonceStr", MD5Util.MD5Encode(randomStr, "utf-8").toLowerCase());
//		nativeObj.put("package", "prepay_id=" + prepay_id);
//		nativeObj.put("signType", "MD5");
//		nativeObj.put("paySign", createSign(nativeObj, appKey));
//
//		System.out.println(JSON.toJSONString(nativeObj));
//
//		return JSON.toJSONString(nativeObj);
//	}
//
//	/**
//	 * 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
//	 */
//	public static String createSign(SortedMap<String, String> packageParams, String AppKey) {
//		StringBuffer sb = new StringBuffer();
//		Set es = packageParams.entrySet();
//		Iterator it = es.iterator();
//		while (it.hasNext()) {
//			Map.Entry entry = (Map.Entry) it.next();
//			String k = (String) entry.getKey();
//			String v = (String) entry.getValue();
//			if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
//				sb.append(k + "=" + v + "&");
//			}
//		}
//		sb.append("key=" + AppKey);
//		String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
//		return sign;
//	}
//}



import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.*;



/**
 * 微信调起支付类
 *
 * @author Sunlight
 *
 */
public class WXPay {
    public static String createPackageValue(String appid, String appKey, String prepay_id)  {
        SortedMap<String, String> nativeObj = new TreeMap<String, String>();
        nativeObj.put("appId", appid);
        nativeObj.put("timeStamp", OrderUtil.GetTimestamp());
        Random random = new Random();
        String randomStr = MD5.GetMD5String(String.valueOf(random.nextInt(10000)));
        nativeObj.put("nonceStr", MD5Util.MD5Encode(randomStr, "utf-8").toLowerCase());
        nativeObj.put("package", "prepay_id=" + prepay_id);
        nativeObj.put("signType", "MD5");
        nativeObj.put("paySign", createSign(nativeObj, appKey));

        System.out.println(JSON.toJSONString(nativeObj));

        return JSON.toJSONString(nativeObj);
    }

    /**
     * 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
     */
    public static String createSign(SortedMap<String, String> packageParams, String AppKey) {
        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + AppKey);
        String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
        return sign;
    }



    public class WxPayConfig {
        //小程序appid
        //微信支付的商户id
//        public static final String mch_id = "1603451149";
    	public static final String mch_id = "1605505176";
        //微信支付的商户密钥
//        public static final String key = "KabJoQkrQoLMns1U46WsCjFfM63HF99o";
    	public static final String key = "4Zf1i0HvkJJEj7URaIgcVYTaTG6fjE83";
        //支付成功后的服务器回调url "https://??/??/weixin/api/wxNotify";
        public static final String notify_url = "https://meituanfanli.net/app/mini_program/wxNotify";
        //签名方式，固定值
        public static final String SIGNTYPE = "MD5";
        //交易类型，小程序支付的固定值为JSAPI
        public static final String TRADETYPE = "JSAPI";
        //微信统一下单接口地址
        public static final String pay_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    }

    /**
     * StringUtils工具类方法
     * 获取一定长度的随机字符串，范围0-9，a-z
     * @param length：指定字符串长度
     * @return 一定长度的随机字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    /**
     * IpUtils工具类方法
     * 获取真实的ip地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }


}

